window.onload = function() {
	var stocks = [];
	var content = document.getElementById("demo");	
	submitForm();
	//setInterval("AJAX_JSON_Req()", 10000);
	setInterval("submitForm()", 2000);
	
		  

}

function AJAX_JSON_Req(req)
{


	    employe = JSON.parse(req);
	    display(employe);
	  

	
}
function json(){
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", './convertdb.php', true);
	var json = xmlhttp.responseText;
	document.getElementById("demo").innerHTML = json;
	xmlhttp.send();
}
function createInstance()
{
  var req = null;
  if(window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
       try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            alert("XHR not created");
          }
      }
    }
    return req;
}
function storing(data, element)
{
	element.innerHTML = data;
}
function submitForm()
{ 
   var req = createInstance();
   req.onreadystatechange = function()
   { 
      if(req.readyState == 4)
      {
         if(req.status == 200)
         {
//storing(req.responseText, element);
        	 AJAX_JSON_Req(req.responseText);
        	 
         }	
         else	
         {
            document.ajax.dyn.value="Error: returned status code " + req.status + " " + req.statusText;
         }	
      } 
   }; 
	 req.open("GET", "./convertdb.php", true); 
	 req.send(null); 
 } 
function display(employe){
	var out = "";
    var i;
    for(i = 0; i < employe.length; i++) {
    	
    		out += '<div class="other"><p> ' + employe[i].login + ' a ecrit : '+ employe[i].message +'</p></div>';
    	
        }
    
    document.getElementById("demo").innerHTML = out;
    var objDiv = document.getElementById("demo");
	objDiv.scrollTop = objDiv.scrollHeight;
}
function add(){
	var msg=document.getElementById('msg').value;
	var nom=document.getElementById('cache').value;
	 $.post(
	       'send_msg.php',
	      {
	       utilisateur: nom,
	       msg : msg
	       }, 
	       function(data){
	    	   /*
	    	    
	    	    if(data==success){
	    		   alert('message envoyÃ©');
	    	   	}
	    	   else{
	    		   alert('a problem has occured');
	    	   }
	    	  */ 
	       },
	       'text'
	       );
	 document.getElementById('msg').value = "";

}